//
//  TodayListViewController.m
//  UI_A段项目_iDaily Watch
//
//  Created by 刘旭 on 14-10-15.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TodayListViewController.h"
#import "TodayListTableViewCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIButton+WebCache.h"
#import "TodayList.h"
#import "TodayViewController.h"
#import "TodaySecondViewController.h"
#import "TodayListData.h"
#import "MJRefresh.h"
#import "TodayListSubViewController.h"
@interface TodayListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataAarrOne;
@property (nonatomic ,assign)int count;
@end

@implementation TodayListViewController

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    TodayListData * data = [[TodayListData alloc]init];
    
    [data getDataFromBlock:^(NSMutableArray* dataSource) {
        self.dataAarrOne = dataSource;
        [_tableView reloadData];
            }];
    

}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView1];
    [self setupRefresh];

    self.view.backgroundColor = [UIColor blackColor];
    // Do any additional setup after loading the view.
   
}
- (void)tableView1
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;//不显示CELL条
    self.tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = [UIColor blackColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 60, 40);
    [button setBackgroundImage:[UIImage imageNamed:@"TodayList返回"] forState:UIControlStateNormal];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button setTintColor:[UIColor whiteColor]];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:button];
    self.navigationItem.hidesBackButton = NO;
    self.navigationItem.leftBarButtonItem = item;
    
}
- (void)buttonAction
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataAarrOne.count == 0) {
        return 0;
    }
    return _dataAarrOne.count / 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cellOne";
    TodayListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"97.jpeg"]];
    cell.alpha = 0.7;
    if (!cell) {
        cell = [[TodayListTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    cell.selectionStyle = NO;
    
    for (int i = 0; i < 5; i++) {
       cell.button = (UIButton *)[cell viewWithTag:i + 1];
        cell.button.layer.transform = CATransform3DMakeRotation(1, 0, 0, 0);
        
        [cell.button setBackgroundImageWithURL:[NSURL URLWithString:[_dataAarrOne[(indexPath.row) * 5 + i]cover_thumb]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"t11.png"]];
        cell.label1 = (UILabel *)[cell viewWithTag:i + 6];
        cell.label1.text = [_dataAarrOne[(indexPath.row) * 5 + i]title];
        //cell.backgroundColor = [UIColor blackColor];
        cell.label2 = (UILabel *)[cell viewWithTag:i + 11];
        cell.label2.text = [_dataAarrOne[(indexPath.row) * 5 + i]pubdate];
          [cell.button  addTarget:self action:@selector(buttonActionSub) forControlEvents:UIControlEventTouchUpInside];
    }
    return cell;
}
- (void)buttonActionSub
{
    NSLog(@"123123123");
    TodaySecondViewController *vc = [[TodaySecondViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
//    TodayListSubViewController *vc = [[TodayListSubViewController alloc]init];
//    [self.navigationController pushViewController:vc animated:YES];
//    [vc release];
}

#pragma mark -
#pragma mark 刷新
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //[self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
//    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
//    [self.tableView footerEndRefreshing];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.headerPullToRefreshText = @"下拉以上弦...";
    self.tableView.headerReleaseToRefreshText = @"松开即可上弦...";
    self.tableView.headerRefreshingText = @"上弦中...";
    
    self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.tableView.footerRefreshingText = @"加载中";
}
#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        //[self.tableView1 reloadData];
        
        
        [_tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView headerEndRefreshing];
    });
}
- (void)footerRereshing
{
    
    // NSMutableArray *  datamodel = [NSMutableArray array];
   
            
       
    self.dataAarrOne = [NSMutableArray array];
    
    AFHTTPRequestOperationManager *mananger1 = [AFHTTPRequestOperationManager manager];
    [mananger1 GET:@"http://watch-cdn.idailywatch.com/api/list/cover/zh-hans?page=1&ver=iphone&app_ver=9" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        for (NSDictionary *dic in responseObject) {
            TodayList *list = [[TodayList alloc]init];
            list.title = [dic objectForKey:@"title"];
            list.pubdate = [dic  objectForKey:@"pubdate"];
            list.cover_thumb = [dic objectForKey:@"cover_thumb"];
            [_dataAarrOne addObject:list];
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

    
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView footerEndRefreshing];
        _count += 1;
    });
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
